package com.qfedu.userserve.controller;


import com.qfedu.entity.Admin;
import com.qfedu.userserve.service.AdminService;
import com.qfedu.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.UUID;

/**
 * @Author niuniu
 * @Date 2024/9/7 11:37
 * @declaration 以梦为马，破茧重生
 */
@RestController
@RequestMapping("/admin/user")
public class AdminController {

    @Autowired
    private AdminService service;
    @PostMapping("/login")
    public R login(@RequestBody Admin admin, HttpSession session){
        System.out.println("TotalSalesController.login,接收数据"+admin);
        Admin login = service.login(admin);
        if (login != null) {
            //登录成功，存储用户信息
            session.setAttribute("userInfo",login);
            //造假token，给前端
            HashMap<String,String> map = new HashMap<>();
            map.put("token", UUID.randomUUID().toString());
            return R.ok(map);
        }
        return R.fail();
    }

    @GetMapping("/info")
    public R login(HttpSession session){
        Admin info = (Admin) session.getAttribute("userInfo");
        return R.ok(info);

    }

    /* 退出系统 */
    @PostMapping("/logout")
    public R logout(HttpSession session){
        session.invalidate();
        return R.ok();
    }

}
